package com.dataex.frame;

import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.dataex.service.DBService;
import com.dataex.service.TableToXmlService;

public class ExportFrame extends JFrame implements ItemListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	String dbName = "jdbca";

	private List<String> list2;
	
	private String[] arrs2 = {};
	
	private DefaultComboBoxModel model;
	
	private JComboBox comboBox1;
	private JComboBox comboBox2;
	
	private final static String BASEURL="../xmlDemo/images/";
	
	private String tableName = "gxpt_czrk_photo";
	
	public ExportFrame(){
		try {
			export();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void export() throws Exception{
     	Image image=Toolkit.getDefaultToolkit().getImage(BASEURL+"logo.png");
 		setIconImage(image);
     	setTitle("导出信息");
     	setLayout(new FlowLayout(FlowLayout.LEFT));
     	
     	JPanel panel1 = new JPanel();
     	
     	JPanel panel2 = new JPanel();
     	
     	JLabel label1 = new JLabel("数据库:");
     	
     	List<String> list1 = new DBService().getAllDatabases();
     	
     	String arrs1[] = new String[list1.size()];
     	
     	for(int i = 0; i < list1.size(); i++){
     		arrs1[i] = list1.get(i);
     	}
     	
     	comboBox1 = new JComboBox(arrs1);
     	comboBox1.setSelectedItem(dbName);
     	comboBox1.addItemListener(this);
     	
     	panel1.add(label1);
     	panel1.add(comboBox1);
     	
     	list2 = new DBService().getTables(dbName);
     	
     	arrs2 = new String[list2.size()];
     	
     	for(int i = 0; i < list2.size(); i++){
     		arrs2[i] = list2.get(i);
     	}
     	
     	JLabel label2 = new JLabel("数据表:");
     	
     	model = new DefaultComboBoxModel();
     	
		try {
			list2 = new DBService().getTables(dbName);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
     	
     	arrs2 = new String[list2.size()];
     	
     	for(int i = 0; i < list2.size(); i++){
     		arrs2[i] = list2.get(i);
     		model.addElement(arrs2[i]);
     	}
     	
     	comboBox2 = new JComboBox();
     	comboBox2.setSelectedItem(tableName);
     	comboBox2.setModel(model);
     	comboBox2.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				// TODO Auto-generated method stub
				if(e.getStateChange()==ItemEvent.SELECTED){
					tableName = "" + e.getItem();
				}
			}
		});
     	
     	
     	panel2.add(label2);
     	panel2.add(comboBox2);
     	
     	JButton btn = new JButton("导出");
     	
     	btn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					if(dbName.equals("jdbca")){
						new TableToXmlService().exportDataToXMlFile("jdbca", "gxpt_czrk_photo");
					}else{
						JOptionPane.showConfirmDialog(null, "当前版本只支持特定数据库","温馨提示",JOptionPane.YES_NO_OPTION);
					}
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
     	
     	add(panel1);
     	add(panel2);
     	add(btn);
     	
     	setVisible(true);
     	setSize(400,400);
     	setLocation(300,300);
	}
	
	
	
	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		if(e.getStateChange()==ItemEvent.SELECTED){//选中JComboBox组件
			dbName = ""+e.getItem();//获取字符串
			try {
				list2 = new DBService().getTables(dbName);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	     	//@SuppressWarnings("unchecked")
			model = (DefaultComboBoxModel)comboBox2.getModel();
			
	     	while(model.getSize()>0){
	     		model.removeElementAt(model.getSize()-1);
	     	}
	     	
	     	arrs2 = new String[list2.size()];
	     	
	     	for(int i = 0;i<list2.size();i++){
	     		arrs2[i] = list2.get(i);
	     		model.addElement(arrs2[i]);
	     	}
		}
	
	}

}
